package com.bdqn.mzc.controller;

import com.bdqn.mzc.model.*;
import com.bdqn.mzc.model.dto.CreateAccountDto;
import com.bdqn.mzc.model.dto.LoginDto;
import com.bdqn.mzc.model.dto.PayDto;
import com.bdqn.mzc.model.dto.UpdateUserDto;
import com.bdqn.mzc.service.UserService;
import com.bdqn.mzc.utils.AliPay;
import com.bdqn.mzc.utils.JWTUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录功能  " +
            " 可以通过邮箱和Gofar账号两种方式进行登录，前端需要进行非空验证")
    @PostMapping("/login")
    @CrossOrigin
    public Result<String> login(@RequestBody LoginDto body) throws Exception {
        User user = userService.login(body.getUsername(),body.getPassword());
        Map<String, String> payload = new HashMap<>();
        String userId = String.valueOf(user.getUserId());
        payload.put("userId", userId);
        //生成令牌
        String token = JWTUtils.getToken(payload);
        return Result.wrapSuccessfulResult(token);
    }

    @ApiOperation("查询所有用户的基本信息")
    @PostMapping("/selectList")
    @CrossOrigin
    public Result<List<User>> selectList(){
        return Result.wrapSuccessfulResult(userService.selectList());
    }


    @ApiOperation("注册功能  " +
            " 前端需验证邮箱格式和密码格式，以及两次输入密码是否相同，验证码不能为空")
    @PostMapping("/register")
    @CrossOrigin
    public Result<Integer> register(@RequestBody CreateAccountDto createAccountDto) throws Exception{
        return Result.wrapSuccessfulResult(userService.register(createAccountDto.getEmail(),createAccountDto.getPassword(),createAccountDto.getCode()));
    }



    @ApiOperation("发送验证码功能  " +
            " 用于注册用户使用，前端需验证邮箱格式是否合法并且邮箱不能为空")
    @GetMapping("/sandcode")
    @CrossOrigin
    public Result<String> sandCode(String email) throws Exception{
      return Result.wrapSuccessfulResult(userService.sandCode(email));
    }

    @GetMapping("/acceptpay")
    public void acceptPay(){
        System.out.println("支付成功");
    }



    @ApiOperation("查询已登录用户信息功能  " +
            " 请求地址前面带user默认经过拦截器进行判断是否登录")
    @GetMapping("/user/personal")
    @CrossOrigin
    public Result<User> queryPersonal(HttpServletRequest request){
        return Result.wrapSuccessfulResult(userService.selectPersonalById(JWTUtils.getUserId(request)));
    }


    @ApiOperation("修改已登录用户个人信息功能  " +
            " 请求地址前面带user默认经过拦截器进行判断是否登录" +
            " 前端需要进行非空验证,格式验证和一些约束" +
            " email  gofar账号 等个别信息不能进行修改")
    @PutMapping("/user/personal")
    public Result<Integer> updatePersonalById(UpdateUserDto updateUserDto, HttpServletRequest request){
       return Result.wrapSuccessfulResult(userService.updatePersonal(updateUserDto, JWTUtils.getUserId(request)));
    }

    @ApiOperation("购票功能 ")
    @GetMapping("/user/ticket")
    public Result<Integer> ticket(HttpServletRequest request,HttpServletResponse response,Integer id) throws Exception {
        return Result.wrapSuccessfulResult(userService.buyTicket(response,request,id));
    }

    @ApiOperation("查询个人订单功能")
    @GetMapping("/user/indent")
    public Result<List<Indent>> indentAll(){
        return Result.wrapSuccessfulResult(userService.selectindentAll());
    }

    @ApiOperation("退票功能  " +
            " 修改订单状态 1代表已付款  2代表已退款 3代表已完成 ")
    @PutMapping("/user/refund")
    public Result<Integer> refund(Integer id){
        return Result.wrapSuccessfulResult(userService.updateIndentById(id));
    }

    @ApiOperation("删除订单功能  " +
            " 订单状态为3的订单才进行此操作")
    @DeleteMapping("/user/delete")
    public Result<Integer> delete(Integer id) throws Exception{
        return Result.wrapSuccessfulResult(userService.deleteIndentById(id));
    }

    @ApiOperation("确认订单功能 " +
            "确认订单后 才能执行 评论和删除等操作")
    @GetMapping("/user/ok")
    public Result<Integer> affirmOrder(Integer id){
        return Result.wrapSuccessfulResult(userService.updateIndentById2(id));
    }


    @ApiOperation("评论功能  " +
            " 订单状态为3的订单才进行此操作")
    @PostMapping("/user/comment")
    public Result<Integer> comment(Integer IndentId,double grade,String content) throws Exception{
        return Result.wrapSuccessfulResult(userService.insertValuate(IndentId,grade,content));
    }
}
